﻿using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using WebDocDB.DocumentDBManager;
using WebDocDB.Managers;
using WebDocDB.Models;

namespace WebDocDB.Controllers
{
    public class CollectionsController : Controller
    {
        private IAccountManager accountManager;
        private IDocumentCollectionsManager documentCollectionsManager;        

        public CollectionsController(IAccountManager accountManager, DocumentCollectionsManager documentCollectionsManager)
        {
            this.accountManager = accountManager;
            this.documentCollectionsManager = documentCollectionsManager;           
        }

        // GET: Collections
        public async Task<ActionResult> Index(string databaseLink)
        {
            if (accountManager.GetAccountSettings() != null)
            {               
                var collections = await documentCollectionsManager.GetAllDatabaseCollections(databaseLink);
                return View(collections.OrderBy(db => db.ID).ToList());
            }

            return RedirectToAction("Index", "Home");

        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public async Task<ActionResult> CollectionsList(string databaseLink)
        {
            if (accountManager.GetAccountSettings() != null)
            {                
                var collections = await documentCollectionsManager.GetAllDatabaseCollections(databaseLink);
                return PartialView("_CollectionsList", collections.OrderBy(db => db.ID).ToList());
            }

            return RedirectToAction("Index", "Home");
        }

        // GET: Collections/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Collections/Create
        public ActionResult Create(string databaseLink, string databaseID)
        {
            var collModel = new Models.DocumentCollection();
            collModel.DatabaseLink = databaseLink;
            collModel.DatabaseID = databaseID;
            return PartialView("_Create", collModel);
        }

        // POST: Collections/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public async Task<JsonResult> Create(Models.DocumentCollection collModel)
        {
            if (ModelState.IsValid)
            {
                if (accountManager.GetAccountSettings() != null)
                {
                    try
                    {                        
                        await documentCollectionsManager.AddDocumentCollection(collModel.DatabaseLink, collModel.ID);
                        return Json(new AjaxResponse() { Success = true, Values = new { DatabaseID = collModel.DatabaseID, DatabaseLink = collModel.DatabaseLink } });
                    }
                    catch (Exception ex)
                    {
                        return Json(new AjaxResponse() { Success = false, Message = ex.Message });
                    }
                    
                }                
            }

            return Json(new AjaxResponse() { Success = false, Message = "Invalid data" });
        }        

        
        // POST: Collections/Delete/5
        [HttpPost]
        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public async Task<JsonResult> Delete(string databaseLink, string databaseID, string collectionLink)
        {
            if (accountManager.GetAccountSettings() != null)
            {
                try
                {                    
                    await documentCollectionsManager.DeleteDocumentCollection(collectionLink);
                    return Json(new AjaxResponse() { Success = true, Values = new { DatabaseID = databaseID, DatabaseLink = databaseLink } });
                }
                catch (Exception ex)
                {
                    return Json(new AjaxResponse() { Success = false, Message = ex.Message });
                }
               
            }

            return Json(new AjaxResponse() { Success = false, Message = "Invalid data" });
        }
    }
}
